#include<bits/stdc++.h>
using namespace std;
const int K = 1000;
int n,q,c;
int a[200011];
int siz[200011];
vector<int> v[200011],son[200011];
int ins[203][1<<15|3];
int dui[200011],m;
int s[1<<15|3];
void DFS(int p,int fa)
{
	siz[p] = 1;
	for(auto i:v[p]) if(i != fa)
	{
		DFS(i,p);
		if(siz[i])
			son[p].push_back(i),siz[p] += siz[i];
	}
	if(siz[p] >= K || !fa)
		dui[p] = ++m,siz[p] = 0;
}
void CDFS(int p,int fa,int S)
{
	s[S]++;
	for(auto i:v[p]) if(i != fa)
		CDFS(i,p,S | (1 << a[i]));
}
void Calc(int p)
{
	memset(s,0,sizeof(s));
	CDFS(p,0,(1 << a[p]));
	for(int k = 0;k < c;k++)
		for(int S = 0;S < (1 << c);S++) if(S & (1 << k))
			s[S] += s[S ^ (1 << k)];
	memcpy(ins[dui[p]],s,sizeof(ins[dui[p]]));
}
int ans,rt;
void Solve(int p,int fa,int S)
{
	if(dui[p])
		return rt = dui[p],void();
	ans++;
	for(auto i:v[p]) if(i != fa && (S & (1 << a[i])))
		Solve(i,p,S);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	cin >> n >> q >> c;
	for(int i = 1;i <= n;i++)
		cin >> a[i];
	for(int i = 2;i <= n;i++)
	{
		int x,y = i;
		cin >> x;
		v[x].push_back(y),v[y].push_back(x);
	}
	DFS(1,0);
	for(int i = 1;i <= n;i++) if(dui[i])
		Calc(i);
	while(q--)
	{
		int S,x;
		cin >> S >> x;
		if(!(S & (1 << a[x])))
		{
			cout << 1 << "\n";
			continue;
		}
		rt = ans = 0;
		Solve(x,0,S);
		cout << (rt ? ins[rt][S] : ans) << "\n";
	}
	return 0;
}
